package com.admin.service.impl;

import com.admin.common.enums.MemberLevelEnum;
import com.admin.common.exception.BizException;
import com.admin.common.util.DateUtils;
import com.admin.domain.AdminDO;
import com.admin.domain.DeclareSampleRecord;
import com.admin.mapper.AdminMapper;
import com.admin.mapper.DeclareMapper;
import com.admin.mapper.DeclareSampleRecordMapper;
import com.admin.service.AdminService;
import com.admin.service.DeclareService;
import com.admin.service.SampleRecordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author yangzhongliang
 * @date 2022/05/16
 */
@Slf4j
@Service
public class SampleRecordServiceImpl implements SampleRecordService {
    @Autowired
    private DeclareSampleRecordMapper declareSampleRecordMapper;
    @Autowired
    private DeclareMapper declareMapper;
    @Autowired
    private AdminService adminService;
    @Autowired
    private AdminMapper adminMapper;

    /**
     * 是否已抽过样
     * @param startTime
     * @param endTime
     * @param sampleType
     * @return
     */
    @Override
    public Boolean isSample(Integer startTime, Integer endTime, Integer sampleType, String sampleLevel) {
        DeclareSampleRecord sampleRecord = declareSampleRecordMapper.getBy(startTime, endTime, sampleType, sampleLevel);
        if (Objects.isNull(sampleRecord)) {
            return false;
        }
        return true;
    }

    /**
     * 查询时间区间内 - 不同申报类型 - 是否已经有 抽样标记 有 则已抽过，
     * @param userId
     * @param startTime
     * @param endTime
     * @param sampleType
     * @return
     */
    @Override
    public Boolean isSampled(Integer userId, Integer startTime, Integer endTime, Integer sampleType) {
        AdminDO adminDO = adminMapper.selectByPrimaryKey(userId);
        if (Objects.isNull(adminDO) || adminDO.getRole() == 1 || StringUtils.isEmpty(adminDO.getBusinessAreaIds())) {
            throw new BizException(-1, "当前用户无抽样权限!");
        }
        Map<String,Object> params = new HashMap<>();
        params.put("startTime", DateUtils.dateFormatStr(startTime));
        params.put("endTime", DateUtils.dateFormatStr(endTime));
        params.put("declareType", sampleType);
        //市抽样
        if (adminDO.getMemberLevel().equals(MemberLevelEnum.CITY.getKey())) {
            params.put("cityIds", adminService.getResponsibleAreaIds());
            params.put("isCitySample", 1);
            //省抽样
        } else if (adminDO.getMemberLevel().equals(MemberLevelEnum.PROVINCE.getKey())) {
            params.put("provinceIds", adminService.getResponsibleAreaIds());
            params.put("isProvinceSample", 1);
        }
        int i = declareMapper.countByMap(params);
        return i > 0?true:false;
    }

    @Override
    public Boolean isSampled(Integer userId, Integer startTime, Integer endTime) {
        AdminDO adminDO = adminMapper.selectByPrimaryKey(userId);
        if (Objects.isNull(adminDO) || adminDO.getRole() == 1 || StringUtils.isEmpty(adminDO.getBusinessAreaIds())) {
            throw new BizException(-1, "当前用户无抽样权限!");
        }
        Map<String,Object> params = new HashMap<>();
        params.put("startTime", DateUtils.dateFormatStr(startTime));
        params.put("endTime", DateUtils.dateFormatStr(endTime));
        //市抽样
        if (adminDO.getMemberLevel().equals(MemberLevelEnum.CITY.getKey())) {
            params.put("cityIds", adminService.getResponsibleAreaIds());
            params.put("isCitySample", 1);
            //省抽样
        } else if (adminDO.getMemberLevel().equals(MemberLevelEnum.PROVINCE.getKey())) {
            params.put("provinceIds", adminService.getResponsibleAreaIds());
            params.put("isProvinceSample", 1);
        }
        int i = declareMapper.countByMap(params);
        return i > 0?true:false;
    }

    @Transactional
    @Override
    public Boolean saveCitySample(Integer userId,  List<Integer> declareIds, DeclareSampleRecord sampleRecord) {
        //更新主表 市级抽样标识
        int i = declareMapper.updateCitySample(declareIds);
        if (i != declareIds.size()) {
            throw new BizException(-1, "更新市级抽样标识异常!");
        }
        //插入日志
        declareSampleRecordMapper.insertSelective(sampleRecord);
        return true;
    }

    @Override
    public Boolean saveProvinceSample(Integer userId, List<Integer> declareIds, DeclareSampleRecord sampleRecord) {
        if (CollectionUtils.isEmpty(declareIds)) {
            return false;
        }
        //更新主表 市级抽样标识
        int i = declareMapper.updateProvinceSample(declareIds);
        if (i != declareIds.size()) {
            throw new BizException(-1, "更新省级抽样标识异常!");
        }
        //插入日志
        declareSampleRecordMapper.insertSelective(sampleRecord);
        return true;
    }
}
